package ysq.xyg.sys.controller;

import cn.hutool.core.util.StrUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;
import ysq.xyg.base.controller.BaseController;
import ysq.xyg.base.model.dto.ReturnEntity;
import ysq.xyg.sys.model.po.SysRole;
import ysq.xyg.sys.model.po.SysRoleMenu;
import ysq.xyg.sys.model.vo.SysRoleMenuVo;
import ysq.xyg.sys.model.vo.SysRoleVo;
import ysq.xyg.sys.service.SysRoleMenuService;
import ysq.xyg.sys.service.SysRoleService;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 系统部分-字典controller层
 *
 * @author shengQiang yu
 * @date 2019-3-28 11:50:53
 */
@RestController
public class SysRoleController extends BaseController {

    private static Logger log= LoggerFactory.getLogger(SysRoleController.class);
    @Resource
    private SysRoleService sysRoleService;
    @Resource
    private SysRoleMenuService sysRoleMenuService;
    private SysRole sysRole;

    /**
     * 分页查询
     *
     * @param sysRoleVo 对象
     * @param pageSize 展示数量
     * @param pageNum 当前页面
     * @author shengQiang yu
     * @date 2019-3-28 11:50:53
     */
    @RequestMapping(value = "/sysRole/page",method = RequestMethod.POST)
    public ReturnEntity page(SysRoleVo sysRoleVo, int pageSize, int pageNum){
        ReturnEntity returnEntity = sysRoleService.page(sysRoleVo,pageSize,pageNum);
        return returnEntity;
    }

    /**
     * 跳转连接
     *
     * @param id 主键id
     * @author shengQiang yu
     * @date 2019-3-28 11:50:53
     */
    @RequestMapping(value = "/sysRole/getEntity",method = RequestMethod.GET)
    public ModelAndView getEntity(String id){
        ModelAndView view = new ModelAndView("/sys/sysRoleUpdateOrSave");
        if(StrUtil.isNotEmpty(id)){
            sysRole = sysRoleService.selectByPrimaryKey(id);
            view.addObject("sysRole", sysRole);
            List<SysRoleMenu> roleMenuList=sysRoleMenuService.acceptList(new SysRoleMenuVo(id));
            String str="";
            if(roleMenuList!=null&&roleMenuList.size()>0){
                for(SysRoleMenu sysRoleMenu:roleMenuList){
                    if(StrUtil.isNotEmpty(str)){
                        str+=","+sysRoleMenu.getMenuId();
                    }else{
                        str=sysRoleMenu.getMenuId();
                    }
                }
            }
            sysRole.setMenuIds(str.toString());
        }
        return view;
    }
    /**
     * 删除
     *
     * @param id 主键id
     * @author shengQiang yu
     * @date 2019-3-28 11:50:53
     */
    @RequestMapping(value = "/sysRole/delete")
    public ReturnEntity delete(HttpServletRequest request,String id){
        return sysRoleService.delete(id,acceptCurrentUser(request));
    }

    /**
     * 添加或修改
     *
     * @param sysRoleVo Vo对象
     * @author shengQiang yu
     * @date 2019-3-28 11:50:53
     */
    @RequestMapping(value = "/sysRole/addOrUpdate",method = RequestMethod.POST)
    public ReturnEntity addOrUpdate(HttpServletRequest request,SysRoleVo sysRoleVo){
        return sysRoleService.addOrUpdate(sysRoleVo,acceptCurrentUser(request));
    }
}